﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Text;
using System.Threading.Tasks;
using KongMing.ORM.SqlSugar.Models;
using SqlSugar;

namespace KongMing.ORM.SqlSugar.DynamicExpression
{
    internal class 动态表达式
    {
        public static void Go()
        {
            using (var db = SqlSugarHelper.GetDB())
            {
                string title = "三体";//定义模糊查询的值
                decimal inPrice = 0;

                //1.表达式连写
                var exp1 = Expressionable.Create<Book>()
                    .AndIF(!string.IsNullOrWhiteSpace(title), x => x.Title.Contains(title))
                    .AndIF(inPrice > 0, x => x.InPrice == inPrice).ToExpression();

                //2.分开写法
                var exp2 = Expressionable.Create<Book>();
                if (!string.IsNullOrWhiteSpace(title))
                    exp2.And(x => x.Title.Contains(title));
                if (inPrice > 0)
                    exp2.And(x => x.InPrice == inPrice);

                //3.多表查询
                var exp3 = Expressionable.Create<StoreBookStock, BookStore, Book>()
                    .AndIF(!string.IsNullOrWhiteSpace(title), (x, y, z) => z.Title.Contains(title))
                    .And((x, y, z) => y.Name.Contains("科幻"));

                var list1 = db.Queryable<Book>().Where(exp1).Where(x => x.IsDelete == 0).ToList();

                //4. Or
                var expOr = Expressionable.Create<Book>().And(x => x.Title == "三体").Or(x => x.Title == "西游记").ToExpression();
                var list = db.Queryable<Book>().Where(expOr).ToList();
            }
        }

    }
}
